function jcb_image_tagger(pathname,recursive)
% settings
fontsize=10;
border=2;
rotation=false;
extend=false;
visual=false;
replace=true;

imgpattern='*.png';

if nargin>=1
    if nargin < 2
        recursive=false;
    end
    
    if recursive
        subdirs = dir(pathname);
        subdirs = subdirs([subdirs.isdir]);
        subdirs={subdirs.name}';
        
        for ii=1:length(subdirs)
            if ~strcmpi(subdirs{ii},'.') && ~strcmpi(subdirs{ii},'..')
                 jcb_image_tagger(fullfile(pathname,subdirs{ii}),true);
            end
        end
    end
    
    filelist = dir(fullfile(pathname,imgpattern));
    filelist = {filelist.name}';
    
end
if nargin<1
    [filelist,pathname] = jcb_pick_files(imgpattern,'Select winspec files to plot',true,pwd);
end
figure(1);
clf;
set(gcf,'visible','off');
figsize=get(0,'screensize')+[10 10 -20 -20];
for ii=1:length(filelist)
    filename=strrep(fullfile(pathname,filelist{ii}),'K:','\\Ma-fs\shared\Lab4');
    %% read image
    im = imread(filename);
    figure(1);
    
        %% plot image
        set(gcf,'units','pixels');
        set(gcf,'position',figsize);

        %% Create the text mask image & set up axes
        im2=uint8(255*ones(size(im)+(fontsize*2+border)*[~rotation rotation 0]*extend));
        image(im2);
        
        
        set(gca,'units','pixels','position',...
            [(figsize(3)-(size(im2,2)-1))/2 (figsize(4)-(size(im2,1)-1))/2 size(im2,2)-1 size(im2,1)-1],...
            'visible','off')

    %     if exist('t','var')==1 && ishandle(t)
    %         delete(t);
    %     end

        %% Add the text
        t=text('units','pixels','fontsize',fontsize,'string',filename,'Rotation',90*rotation,'interpreter','none');
        textent=get(t,'extent');
        if rotation
            position=[size(im2,2)-(fontsize+border) border]+[0 5];
        else
            position=[size(im2,2)-(textent(3)+border) fontsize+border];
        end
        set(t,'position',position);


        % Capture the text image 
        % Note that the size will have changed by about 1 pixel 
        tim = getframe(gca); 

        % Extract the cdata
        im2 = tim.cdata;
        %% concatenate images
        im3=im2;
        im3(1:min(size(im,1),size(im2,1)),1:min(size(im,2),size(im2,2)),:)=im;
        im3(im2==0)=0;
        axis off
        %% print figure
        disp(filename);
        if replace
            if visual
                image(im3);
                axis off
                pause(0.1);
            end
            imwrite(im3,filename,'png');
        else
            image(im3);
            axis off;
            pause(0.5);
%             imwrite(im3,strrep(filename,'.png','_jtagged.png'),'png');
        end
            
    
end
close(1);
% references:
% http://www.mathworks.com/help/techdoc/ref/image.html
% http://www.mathworks.com/support/solutions/en/data/1-1BALJ/index.html?solution=1-1BALJ
